package winter_vacation_homework.day_14;

import java.util.Stack;

/**
 *  【LeetCode题号: 905. 按奇偶排序数组】
 *  https://leetcode-cn.com/problems/sort-array-by-parity/
 */
public class SortArrayByOddOrEven {
    public int[] sortArrayByParity(int[] nums) {
        Stack<Integer> stackOdd = new Stack<>();
        Stack<Integer> stackEven = new Stack<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] % 2 == 0){
                stackEven.push(nums[i]);
            }else {
                stackOdd.push(nums[i]);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (!stackEven.isEmpty()){
                nums[i] = stackEven.pop();
            }else if (!stackOdd.isEmpty()){
                nums[i] = stackOdd.pop();
            }
        }
        return nums;

    }
}
